import axios from 'axios'

import {
  Toast
} from 'vant'

axios.defaults.baseURL = 'http://localhost:3000'

axios.defaults.withCredentials = true // 让ajax携带cookie

axios.interceptors.response.use(
  res => {
    if (res.data.error === 400) {
      Toast.fail(res.data.message)
      console.log(location)

      location.href = `/#/mobile/login?repath=/${location.hash}`
    }
    return res
  })

// 获取一级分类列表
export const getClassify = () => axios.get(`/category/queryTopCategory`)

// 获取二级分类列表
export const getTwoClassify = id => axios.get(`/category/querySecondCategory`, { params: { id } })

export const mobile = {
  queryTopCategory: () => axios.get('/category/queryTopCategory'),

  // 二级分类查询
  querySecCategory: obj => axios.get('/category/querySecondCategory', {
    params: obj
  }),

  // 获取登陆数据
  getLogin: obj => axios.post('/user/login', obj),

  // 获取购物车的数据
  getCart: obj => axios.get('/cart/queryCartPaging', obj)
}
